"""TAPA Python CLI build configuration."""

# Copyright (c) 2024 RapidStream Design Automation, Inc. and contributors.
# All rights reserved. The contributor(s) of this file has/have agreed to the
# RapidStream Contributor License Agreement.

load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_files", "pkg_mklink", "strip_prefix")
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:packaging.bzl", "py_wheel")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:py_binary.bzl", "py_binary")
load("@tapa_deps//:requirements.bzl", "requirement")
load("//bazel:nuitka_rules.bzl", "nuitka_binary")
load("//bazel:pytest_rules.bzl", "py_test")

package(default_visibility = ["//tapa:__subpackages__"])

compile_pip_requirements(
    name = "requirements",
    src = "requirements.in",
    requirements_txt = "requirements_lock.txt",
)

nuitka_binary(
    name = "nuitka",
    src = ":tapa",
    flags = [
        "--deployment",
        "--include-package-data=pyverilog",
        "--include-package-data=tapa",
        # -c is used by tapa but nuitka incorrectly marks it as self-execution
        "--no-deployment-flag=self-execution",
        # ply depends on docstrings so no_docstrings is not an option
        "--python-flag=isolated,no_site",
    ],
    output_name = "tapa-bin",
)

py_binary(
    name = "tapa",
    srcs = ["__main__.py"],
    data = [
        "//fpga-runtime:include",
        "//fpga-runtime:pkg-extra-runtime-deps",
        "//fpga-runtime:pkg-lib",
        "//tapa-cpp",
        "//tapa-lib:include",
        "//tapa-lib:pkg-extra-runtime-deps",
        "//tapa-lib:pkg-lib",
        "//tapa-system-include",
        "//tapa-system-include:tapa-extra-runtime-include",
    ],
    main = "__main__.py",
    visibility = ["//visibility:public"],
    deps = [
        ":tapa-py-libs",
        "//tapacc",
    ],
)

py_wheel(
    name = "tapa-wheel",
    distribution = "tapa",
    python_tag = "py3",
    version = "0.dev",
    deps = [
        ":tapa-py-libs",
    ],
)

py_library(
    name = "tapa-py-libs",
    deps = [
        ":__init__",
        ":core",
        ":instance",
        ":safety_check",
        ":task",
        ":util",
        "//tapa/assets",
        "//tapa/common",
        "//tapa/steps",
        "//tapa/verilog",
        requirement("click"),
    ],
)

py_library(
    name = "__init__",
    srcs = ["__init__.py"],
    data = [
        ":VERSION",
    ],
)

py_library(
    name = "abc",
    srcs = ["abc.py"],
)

py_library(
    name = "core",
    srcs = ["core.py"],
    deps = [
        ":instance",
        ":safety_check",
        ":synthesis",
        ":task",
        ":util",
        "//tapa/backend",
        "//tapa/verilog",
        "//tapa/verilog/xilinx:module",
        requirement("psutil"),
        requirement("pyverilog"),
        requirement("pyyaml"),
        requirement("toposort"),
    ],
)

py_library(
    name = "instance",
    srcs = ["instance.py"],
    deps = [
        ":util",
        "//tapa/verilog",
        requirement("pyverilog"),
    ],
)

py_library(
    name = "safety_check",
    srcs = ["safety_check.py"],
    deps = [
        ":task",
    ],
)

py_library(
    name = "synthesis",
    srcs = ["synthesis.py"],
    deps = [
        ":abc",
        "//tapa/backend",
        requirement("psutil"),
    ],
)

py_library(
    name = "task",
    srcs = ["task.py"],
    deps = [
        ":__init__",
        ":instance",
        ":util",
        "//tapa/backend",
        "//tapa/verilog",
        "//tapa/verilog/xilinx:module",
        requirement("pyverilog"),
    ],
)

py_test(
    name = "task_test",
    srcs = ["task_test.py"],
    deps = [
        ":task",
    ],
)

py_library(
    name = "util",
    srcs = ["util.py"],
    deps = [
        requirement("coloredlogs"),
    ],
)

py_test(
    name = "util_test",
    srcs = ["util_test.py"],
    deps = [
        ":util",
    ],
)

pkg_files(
    name = "pkg-runtime",
    srcs = ["//tapa:nuitka"],
    attributes = pkg_attributes(mode = "0755"),
    prefix = "usr/share/tapa/runtime",
    strip_prefix = strip_prefix.from_pkg("__main__.dist"),
    visibility = ["//visibility:public"],
)

# link usr/share/tapa/runtime/tapa-bin to /usr/bin/tapa
pkg_mklink(
    name = "pkg-link",
    link_name = "usr/bin/tapa",
    target = "../share/tapa/runtime/tapa-bin",
    visibility = ["//visibility:public"],
)

pkg_filegroup(
    name = "pkg",
    srcs = [
        ":pkg-link",
        ":pkg-runtime",
    ],
    visibility = ["//visibility:public"],
)
